package com.zjw.zy.coreui.fragment.access

import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.widget.Button
import androidx.fragment.app.viewModels
import com.baidu.ocr.sdk.model.IDCardResult
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.bean.User
import com.zjw.zy.constant.AppConstant
import com.zjw.zy.coreui.dialog.LoadingDialog
import com.zjw.zy.ext.*
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.OcrUtils
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.SharePrefUtils
import com.zjw.zy.viewmodel.AccessViewModel

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/7 14:13
 */
class AccessInfoFragment : BaseFragment() {
    companion object {
        private const val TAG = "AccessInfoFragment"
    }

    private val mAccessViewModel: AccessViewModel by viewModels({ requireParentFragment() })

    private val ocrIdentity: Button by lazy { findViewById(R.id.fragment_access_ocr) }
    private val info: WebView by lazy { findViewById(R.id.bind_access_info_text) }

    private val loadingDialog: LoadingDialog by lazy { LoadingDialog(mainActivity()) }

    private var isRegisterSelf = false

    override fun onPageEnter() {
        AppUtils.getSelf()?.let {
            mAccessViewModel.emitUser(it)
        }

        info.open("file:///android_asset/access_info.html")

    }

    override fun onPageDestroy() {
        info.release()
        super.onPageDestroy()
    }


    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

    }


    override fun onBindViewListener() {
        ocrIdentity.setOnClickListener {
            if (isRegisterSelf) {
                (requireParentFragment() as AccessFragment).switchPage(AccessFragment.SUB_BIND_INDEX)
            } else {
                performIdCheck()
            }
        }


        //todo 媳妇的专属通道
        ocrIdentity.setOnLongClickListener {
            "我亲爱的媳妇的专属".longToast()

            mAccessViewModel.registerAccess("511622199810010124", "重庆市", "赵洋")
            (requireParentFragment() as AccessFragment).switchPage(AccessFragment.SUB_BIND_INDEX)
            return@setOnLongClickListener true
        }
    }

    private fun performIdCheck() {
        OcrUtils.startIdentityCramer(object : OcrUtils.IDOCRListener {
            override fun onStart() {
                loadingDialog.title(ResourcesUtils.getString(R.string.access_check_id_loading))
                loadingDialog.show()
            }

            override fun onError() {
                loadingDialog.dismiss()
                "错误".shortToast()
            }

            override fun onResult(result: IDCardResult) {
                loadingDialog.dismiss()
                //处理结果
                try {
                    mAccessViewModel.emitUser(User().apply {
                        name = result.name.words
                        username = result.idNumber.words
                        address = result.address.words
                    })
                } catch (e: java.lang.Exception) {
                    "错误".shortToast()
                    return
                }
                (requireParentFragment() as AccessFragment).switchPage(AccessFragment.SUB_ID_INDEX)
            }
        })
    }

    override fun onBindViewValue() {
        isRegisterSelf = SharePrefUtils.getString(AppConstant.APP_ME_ACCESS_KEY, "").isNotEmpty()


        ocrIdentity.text = if (isRegisterSelf) {
            ResourcesUtils.getString(R.string.access_id_opt_button2)
        } else {
            ResourcesUtils.getString(R.string.access_id_opt_button)
        }
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_info_access
    }

    override fun onPageExit() {

    }

}